<template>
  <div class="login-container">
    <div class="container">
      <p class="qrcode-title">APP扫码登录</p>
      <div ref="qrCodeUrl" class="qrcode" />
      <p class="qrcode-content">请扫码登录</p>
    </div>
    <div class="container">
      <p class="qrcode-title">微信扫码登录</p>
      <div ref="qrCodeUrl2" class="qrcode" />
      <p class="qrcode-content">请扫码登录</p>
    </div>

    <div class="center">
      <el-button v-if="button" @click="gorz">去认证</el-button>
    </div>
  </div>
</template>

<script>
import axios from 'axios';
import QRCode from 'qrcodejs2';

export default {
  name: 'Login',
  data() {
    return {
      button: false,
      i: 0,
      i2: 0,
      text: '',
      text2: '',
      interval1: Object,
      interval2: Object
    };
  },
  mounted() {
    if (
      /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent)
    ) {
      this.button = true;
    }
    this.creatQrCode();
    this.creatQrCode2();
  },
  destroyed() {
    window.clearInterval(this.interval1);
    window.clearInterval(this.interval2);
  },
  methods: {
    gorz() {
      // window.location.href = 'http://www.baidu.com'
      window.location.href =
        'ywkj://tech.hzywkj.net/checkCode?code=' + this.text;
    },
    // * 1、bhyp
    // * 2、wjlh
    // * 3、hfq
    // * 4、jxd
    // * 5、sjzt
    creatQrCode() {
      axios
        .post(
          'https://tech.hzywkj.net//qrCode/getQRCodeWithLogo',
          { channelCode: '2' },
          { headers: { 'Content-Type': 'application/json' } }
        )
        .then(res => {
          if (res.data.data) {
            this.text = res.data.data;
            var text = this.text;
            new QRCode(this.$refs.qrCodeUrl, {
              text: text,
              width: 200,
              height: 200,
              colorDark: '#000000',
              colorLight: '#ffffff',
              correctLevel: QRCode.CorrectLevel.H
            });
          }
          this.check();
        });
    },
    creatQrCode2() {
      var redirect =
        'https://h5.hxr.tongrong365.cn/wx/print.html?appid=wxef779cdb84d38b75&qr=';

      axios
        .post(
          'https://tech.hzywkj.net//qrCode/getQRCodeWithLogo',
          { channelCode: '2' },
          { headers: { 'Content-Type': 'application/json' } }
        )
        .then(res => {
          if (res.data.data) {
            this.text2 = res.data.data;
            redirect = redirect + this.text2;
            const redirectUrl =
              'https://open.weixin.qq.com/connect/oauth2/authorize?' +
              'appid=wxef779cdb84d38b75' +
              '&redirect_uri=' +
              encodeURIComponent(redirect) +
              '&response_type=code&scope=snsapi_base&state=' +
              '0' +
              '#wechat_redirect';
            var text2 = redirectUrl;
            new QRCode(this.$refs.qrCodeUrl2, {
              text: text2,
              width: 200,
              height: 200,
              colorDark: '#000000',
              colorLight: '#ffffff',
              correctLevel: QRCode.CorrectLevel.H
            });
          }
          this.check2();
        });
    },
    check() {
      this.interval1 = window.setInterval(this.result, 500);
    },
    check2() {
      this.interval2 = window.setInterval(this.result2, 500);
    },
    result() {
      if (this.i === 120) {
        alert('扫码结果获取失败,请重新扫码');
        // 重新获取码
        this.i = 0;
        window.clearInterval(this.interval1);
        window.location.reload();
        return;
      }
      this.i++;
      var that = this;
      axios
        .post(
          'https://tech.hzywkj.net/qrCode/getH5QRCode',
          { qrUrl: that.text },
          { headers: { 'Content-Type': 'application/json' } }
        )
        .then(res => {
          // console.log('res=>', res)
          if (res.data) {
            if (res.data.code === 1) {
              console.log(res.data.msg);
            } else if (res.data.code === 2) {
              // todo 跳转正式登录
              console.log(res.data.msg);
              window.clearInterval(this.interval1);
              this.codeLogin();
            } else if (res.data.code === 3) {
              console.log(res.data.msg);
            } else if (res.data.code === 4) {
              console.log(res.data.msg);
            } else {
              console.log(res.data.msg);
            }
          }
        });
    },
    result2() {
      if (this.i2 === 120) {
        alert('扫码结果获取失败,请重新扫码');
        // 重新获取码
        this.i2 = 0;
        window.clearInterval(this.interval2);
        window.location.reload();
        return;
      }
      this.i2++;
      var that = this;
      axios
        .post(
          'https://tech.hzywkj.net/qrCode/getH5QRCode',
          { qrUrl: that.text2 },
          { headers: { 'Content-Type': 'application/json' } }
        )
        .then(res => {
          // console.log('res=>', res)
          if (res.data) {
            if (res.data.code === 1) {
              console.log(res.data.msg);
            } else if (res.data.code === 2) {
              // todo 跳转正式登录
              console.log(res.data.msg);
              window.clearInterval(this.interval2);
              this.codeLogin2();
            } else if (res.data.code === 3) {
              console.log(res.data.msg);
            } else if (res.data.code === 4) {
              console.log(res.data.msg);
            } else {
              console.log(res.data.msg);
            }
          }
        });
    },
    codeLogin() {
      this.loading = true;
      this.$store
        .dispatch('user/codelogin', this.text)
        .then(() => {
          this.$router.push({
            path: this.redirect || '/',
            query: this.otherQuery
          });
          this.loading = false;
        })
        .catch(() => {
          this.$notify({
            title: '错误',
            message: '扫码登录失败,请重新扫码',
            type: 'fail',
            duration: 2000
          });
          this.loading = false;
          this.i = 0;
          // 刷新二维码页面
          window.location.reload();
        });
    },
    codeLogin2() {
      this.loading = true;
      this.$store
        .dispatch('user/codelogin', this.text2)
        .then(() => {
          this.$router.push({
            path: this.redirect || '/',
            query: this.otherQuery
          });
          this.loading = false;
        })
        .catch(() => {
          this.$notify({
            title: '错误',
            message: '扫码登录失败,请重新扫码',
            type: 'fail',
            duration: 2000
          });
          this.loading = false;
          this.i2 = 0;
          // 刷新二维码页面
          window.location.reload();
        });
    }
  }
};
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
.center {
  width: 80px;
  position: relative;
  left: 50%;
  top: -90px;
  margin-left: -40px;
}
.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}

.container {
  width: 302px;
  height: 400px;
  margin: 100px auto;
  padding-top: 10px;
  padding-right: 50px;
  padding-left: 50px;
  padding-bottom: 50px;
  background: #ffffff;
  border-radius: 3px;
}

.qrcode-title {
  text-align: left;
  font-size: 20px;
  margin-left: -20px;
}
.qrcode-content {
  text-align: center;
  margin-top: 40px;
  font-size: 20px;
}
</style>
